package org.snusmumr.android.media.data;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;

public abstract class ContactAPI {
	private static ContactAPI api;
	private static String[] BASIC_CONTACT_PROJECTION;
	
	private ContentResolver cr;
 	
 	public static ContactAPI getAPI() {
 		if (api == null) {
 			Class<? extends ContactAPI> clazz;
 			if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.ECLAIR) {
 				clazz = ContactAPISdk5.class;
 			} else {
 				clazz = ContactAPISdk3.class;
 			}
 			
 			try {
 				api = clazz.newInstance();
 			} catch (Exception e) {
 				throw new IllegalStateException(e);
 			}
 			
 		}
 		return api;
 	}
 	
 	public abstract Intent getContactIntent();
 	
	public ContentResolver getContentResolver() {
		return cr;
	}

	public void setContentResolver(ContentResolver cr) {
		this.cr = cr;
	}
	
	public String[] getBasicContactProjection() {
		if(BASIC_CONTACT_PROJECTION == null) {
			BASIC_CONTACT_PROJECTION = new String[]{
				getMetaData().id(),
				getMetaData().displayName()
			};
		}
		return BASIC_CONTACT_PROJECTION;
	}
	
	public abstract long insertContact(String name);
	
	public abstract Cursor getContactsCursor(String selection, String[] selectionArgs, String sortOrder);
	
	public abstract IContactMetaData getMetaData();
	
	public interface IContactMetaData {
		
		public String id();
		
		public String displayName();

		public Uri getUri();
	}
	
}
